package com.leetcode.August;

import javax.swing.tree.TreeNode;

/**
 * @Description: 230. 二叉搜索树中第 K 小的元素
 * @Author: Mr.Fxy
 * @CreateTime: 2024-09-01 21:11
 */
public class LC230二叉搜索树中第K小的元素 {
    int ans = 0, count = 0;

    public int kthSmallest(TreeNode root, int k) {
        order(root, k);
        return ans;
    }

    public void order(TreeNode root, int k) {
        if (root == null) {
            return;
        }
        order(root.left, k);
        count++;
        if (count == k) {
            ans = root.val;
            return;
        }
        order(root.right, k);
    }
}
